package com.jd.mysql.mgr.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jd.mysql.mgr.dao.MgrDatabaseDAO;
import com.jd.mysql.mgr.pojo.MgrDatabase;
import com.jd.mysql.mgr.pojo.MgrMachine;

public class MgrDatabaseDAOImpl extends HibernateDaoSupport implements
		MgrDatabaseDAO {

	public void deleteMgrDatabase(MgrDatabase mgrDb) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.delete(mgrDb);
	}

	public MgrDatabase findMgrDatabaseById(Integer id) {
		// TODO Auto-generated method stub
		return (MgrDatabase) getHibernateTemplate().get(MgrDatabase.class, id);
	}

	public void saveMgrDatabase(MgrDatabase mgrDb) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.save(mgrDb);
	}

	public void updateMgrDatabase(MgrDatabase mgrDb) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.update(mgrDb);
	}

	public List<MgrDatabase> getMgrDatabaseList(Integer start, Integer limit,Boolean enable,Integer machineId,Integer groupId) {
		// TODO Auto-generated method stub
		String hql = "from MgrDatabase where 1=1 ";
		if(groupId!=-1){
			hql += "and mgrMachine.groupId="+groupId+" ";
		}
		if(enable){
			hql += "and yn=true ";
		}
		if(machineId!=0){
			hql+=" and mgrMachine.id=:machineId ";
		}
		hql+="order by id desc";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setMaxResults(limit);
		query.setFirstResult(start);
		if(machineId!=0){
			query.setInteger("machineId", machineId);
		}
		List<MgrDatabase> list = query.list();
		return list;
	}

	public Long getTotalCount(Boolean enable,Integer machineId,Integer groupId) {
		// TODO Auto-generated method stub
		String hql = "select count(*) from MgrDatabase where 1=1 ";
		if(groupId!=-1){
			hql += "and mgrMachine.groupId="+groupId+" ";
		}
		if(enable){
			hql += "and yn=true ";
		}
		if(machineId!=0){
			hql+="and mgrMachine.id=:machineId ";
		}
		Long totalCount = 0l;
		Session session = getSession();
		Query query = session.createQuery(hql);
		if(machineId!=0){
			query.setInteger("machineId", machineId);
		}
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			totalCount = list.get(0);
		}
		return totalCount;
	}

	public void disableMgrDatabase(MgrDatabase mgrDb) {
		// TODO Auto-generated method stub
		mgrDb.setYn(false);
		mgrDb.setMgrMachine(null);
		this.updateMgrDatabase(mgrDb);
	}

	public List<MgrDatabase> getMgrDatabaseListByMgrDatabase(
			MgrMachine mgrMachine) {
		// TODO Auto-generated method stub
		String hql = "from MgrDatabase where yn=true and mgrMachine.id=:mgrMachineId";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setInteger("mgrMachineId", mgrMachine.getId());
		List<MgrDatabase> list = query.list();
		return list;
	}

	public Long getTotalCountByMgrDatabase(MgrMachine mgrMachine) {
		// TODO Auto-generated method stub
		String hql = "select count(*) as count from MgrDatabase where yn=true and mgrMachine.id=:mgrMachineId";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setInteger("mgrMachineId", mgrMachine.getId());
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			return list.get(0);
		}
		return 0l;
	}

	public List<MgrDatabase> getMgrDatabaseList(Integer start, Integer limit,
			Boolean enable, Integer machineId, Integer groupId, String dbName) {
		// TODO Auto-generated method stub
		String hql = "from MgrDatabase where 1=1 ";
		if(groupId!=-1){
			hql += "and mgrMachine.groupId="+groupId+" ";
		}
		if(enable){
			hql += "and yn=true ";
		}
		if(machineId!=0){
			hql+=" and mgrMachine.id=:machineId ";
		}
		if(dbName!=null){
			hql+="and dbName like '%"+dbName+"%' ";
		}
		hql+="order by id desc";
		Session session = getSession();
		Query query = session.createQuery(hql);
		query.setMaxResults(limit);
		query.setFirstResult(start);
		if(machineId!=0){
			query.setInteger("machineId", machineId);
		}
		List<MgrDatabase> list = query.list();
		return list;
	}

	public Long getTotalCount(Boolean enable, Integer machineId,
			Integer groupId, String dbName) {
		// TODO Auto-generated method stub
		String hql = "select count(*) from MgrDatabase where 1=1 ";
		if(groupId!=-1){
			hql += "and mgrMachine.groupId="+groupId+" ";
		}
		if(enable){
			hql += "and yn=true ";
		}
		if(machineId!=0){
			hql+="and mgrMachine.id=:machineId ";
		}
		if(dbName!=null){
			hql+="and dbName like '%"+dbName+"%' ";
		}
		Long totalCount = 0l;
		Session session = getSession();
		Query query = session.createQuery(hql);
		if(machineId!=0){
			query.setInteger("machineId", machineId);
		}
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			totalCount = list.get(0);
		}
		return totalCount;
	}
	
	

}
